//
//  SongListViewController.m
//  WZLProduct
//
//  Created by boys on 16/3/25.
//  Copyright © 2016年 HaiTeng. All rights reserved.
//

#import "SongListViewController.h"
#import "SongListCollectionViewCell.h"
#import "ZPBLongListModel.h"
#import "ZPBAllViewController.h"
@interface SongListViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,ZPBAllViewControllerDelegate>

@property (nonatomic, strong) UIButton *allButton;   /** 全部按钮 */
@property (nonatomic, strong) UIButton *hotButton;   /** 热度按钮 */
@property (nonatomic, strong) UIButton *NewButton;   /** 最新按钮 */
@property (nonatomic, strong) UIView *sView;         /** 线 */
@property (nonatomic, strong) UIView *bView;         /** 放按钮的视图 */
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *SongListDataSouce; //热歌数据
@property (nonatomic, strong) NSMutableArray *nDataSouce; // 最新数据
/// 控制数据页数
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isUpState;
@property (nonatomic, strong) NSString *nanStr;
@property (nonatomic, strong) NSString *tempStr;

@end

@implementation SongListViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.SongListDataSouce = [NSMutableArray array];
        self.nDataSouce = [NSMutableArray array];
        
            self.tempStr = @"最热";
        _page = 1;
        _isUpState = NO;
        _type = @"";
        
        //        [self getNewData];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"歌单广场";
    [self createCollectionView];
    [self createView];
    

    //获得数据
    [self getSongListData];
    // 下拉刷新
//    [self addHeader];
    // 下拉加载
    [self addFooter];
    

    
}
#pragma mark - 上拉加载
//- (void)addHeader
//{
//    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
//        _page = 1;
//        _isUpState = NO;
//        [self getSongListData];
//    }];
//    self.collectionView.mj_header = header;
//    
//}
#pragma mark - 下拉刷新
- (void)addFooter {
    {
      self.collectionView.mj_footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page++;
            _isUpState = YES;
            [self getSongListData];
        }];
        self.collectionView.mj_header.automaticallyChangeAlpha = YES;
        
    }
}
#pragma mark - 创建collectionView
- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((kScreenWidth - 30) / 2, kScreenHeight / 3);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 60, kScreenWidth, kScreenHeight)collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    [_collectionView registerClass:[SongListCollectionViewCell class] forCellWithReuseIdentifier:@"SongListCollectionViewCell"];
    [self.view addSubview:_collectionView];
    
    
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _SongListDataSouce.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SongListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SongListCollectionViewCell" forIndexPath:indexPath];
    
    ZPBLongListModel *model = _SongListDataSouce[indexPath.row];
    
    cell.titleLabel.text = model.title;
    
    [cell.bgImg sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil];
    
    return cell;
}




- (void)createView {
    self.bView = [[UIView alloc ]initWithFrame:CGRectMake(0, 0, kScreenWidth, 60)];
    _bView.backgroundColor= [UIColor whiteColor];
    [self.view addSubview:_bView];
    
    self.allButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_allButton setTitle:@"全部 >" forState:UIControlStateNormal];
    [_allButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _allButton.layer.borderWidth = 2;
    _allButton.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [_allButton addTarget:self action:@selector(allButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bView addSubview:_allButton];
    [_allButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(15);
        make.bottom.offset(- 15);
        make.width.offset(80);
    }];
    
    self.NewButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    [_NewButton setImage:[UIImage imageNamed:@"iconfont-zuixingonggao"] forState:UIControlStateNormal];
    [_NewButton setTitle:@"最新" forState:UIControlStateNormal];
    _NewButton.titleLabel.font = FONT15;
    [_NewButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [_NewButton addTarget:self action:@selector(NewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bView addSubview:_NewButton];
    [_NewButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.bottom.offset(-20);
        make.right.offset(-30);
        make.width.offset(30);
    }];
    
    
    self.sView = [[UIView alloc] init];
    _sView.backgroundColor = [UIColor blackColor];
    [_bView addSubview:_sView];
    [_sView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_NewButton);
        make.right.equalTo(_NewButton.mas_left).with.offset(-10);
        make.bottom.equalTo(_NewButton);
        make.width.offset(1);
    }];
    
    self.hotButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _hotButton.titleLabel.font = FONT15;
    [_hotButton setTitle:@"最热" forState:UIControlStateNormal];
    [_hotButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_hotButton addTarget:self action:@selector(hotButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_bView addSubview:_hotButton];
    [_hotButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_NewButton);
        make.right.equalTo(_sView.mas_left).with.offset(-10);
        make.bottom.equalTo(_NewButton);
        make.width.equalTo(_NewButton);
    }];
    
}


#pragma mark - 全部歌单按钮点击方法
- (void)hotButtonAction:(UIButton *)button {
    [_hotButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_NewButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//    _type = [NSString stringWithFormat:@"最热|%@", _nanStr];
//    NSLog(@"%@", _type);

    self.tempStr = @"最热";
    self.page = 1;
    
    [_SongListDataSouce removeAllObjects];

    [self getSongListData];
   }


- (void)NewButtonAction:(UIButton *)newbutton {
    [_NewButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [_hotButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
     self.tempStr = @"最新";
    self.page = 1;
    
    [_SongListDataSouce removeAllObjects];
    [_collectionView reloadData];
    
    [self getSongListData];
    
}

//Block回调 返回str值
- (void)allButtonAction:(UIButton *)allButton{
    
    ZPBAllViewController *allVc = [[ZPBAllViewController alloc] init];
    
    allVc.delegate = self;
    
    allVc.block = ^(NSString *str){
        
        self.type = [NSString stringWithFormat:@"%@",str];

        NSString *themeStr = [NSString stringWithFormat:@"%@ >", _type];
        
        self.type = [NSString stringWithFormat:@"|%@",str];
        
        [_allButton setTitle:themeStr forState:UIControlStateNormal];
        
        
//        [_allButton addTarget:self action:@selector(allButtonAction:) forControlEvents:UIControlEventTouchUpInside];

        
        [self getSongListData];
        
    };
    
    [self.navigationController pushViewController:allVc animated:YES];
}

//协议传值
- (void)withNSString:(NSString *)str{
    
    _tempStr = str;
    _type = @"";
    _page = 1;
    
    [_allButton setTitle:@"全部" forState:UIControlStateNormal];
    
    [self getSongListData];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - 数据解析
- (void)getSongListData{


    
    NSString *path = [NSString stringWithFormat:@"http://search.dongting.com/songlist/search?q=tag:%@%@&page=%ld&size=10",_tempStr,_type, (long)_page];

     // 判断 包含 %7C = |
    if ([path containsString:@"|"]) {
        NSString *strUrl = [path stringByReplacingOccurrencesOfString:@"|" withString:@"%7C"];
        path = strUrl;
        
    }
    
    NSString *newStr = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:path]]; //有中文,需转码
    NSLog(@"%@", path);
    
    [RequestData GETRequestWithURLString:newStr completionWithSuccess:^(id result) {
 
        [self setData:result];
    } WithFailure:^(id error) {
//        [self setData:error];
    }];
}

- (void)setData:(id)data{
    if (_isUpState == NO) {
        
        [_SongListDataSouce removeAllObjects];
        
    }
    
    for (NSDictionary *dic in data[@"data"]) {
        ZPBLongListModel *model = [[ZPBLongListModel alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.SongListDataSouce addObject:model];
    }

    [_collectionView reloadData];
    
    [_collectionView.mj_footer endRefreshing];

}



@end
